//Global Variabels
dim xml_LinesHolder[] as string

//Functions
function xml_SetupFile(path as string)

    fileid = OpenToRead(path)

    xml_MaxLines = 0

    while FileEOF(fileid) = 0

        currentline$ = ReadLine(fileid)
        inc xml_MaxLines, 1

    endwhile

    CloseFile(fileid)

endfunction xml_MaxLines

function xml_LoadFile(path as string, maxLines as integer)

    dim xml_LinesHolder[maxLines] as string

    fileid = OpenToRead(path)

    current = 0

    while FileEOF(fileid) = 0

        currentline$ = ReadLine(fileid)
        xml_LinesHolder[current] = currentline$
        inc current, 1


    endwhile

    CloseFile(fileid)

endfunction

function xml_getElement(iterator as integer)
    returnValue as string

    pos_a = str_find(xml_LinesHolder[iterator],"</",0)
    if pos_a = -1
        pos_a = str_find(xml_LinesHolder[iterator],"<",0)
        inc pos_a, 1
    else
        inc pos_a, 2
    endif

    pos_b = str_find(xml_LinesHolder[iterator],">",pos_a)
    if pos_b = -1
        pos_b = str_find(xml_LinesHolder[iterator],"/>",pos_a)
    endif

    pos_c = str_find(xml_LinesHolder[iterator]," ",pos_a)
    if pos_c < pos_b and pos_c > -1
        pos_b = pos_c
    endif

    returnValue = str_substr(xml_LinesHolder[iterator],pos_a,pos_b)
endfunction returnValue

function xml_getElementContent(element as string, iterator as integer)
    content as string
    pos_a as integer
    pos_b as integer

    pos_a = str_find(xml_LinesHolder[iterator],element,0)

    if pos_a = -1
        content = "NULL"
    else
        pos_b = str_find(xml_LinesHolder[iterator],element,pos_a+1)
    endif

    if pos_b = -1 or pos_a = -1
        content = "NULL"
    else
        content = str_substr(xml_LinesHolder[iterator],pos_a+len(element)+1,pos_b-2)
    endif
endfunction content

function xml_getAttributeContent(attribute as string, iterator as integer)
    pos_a as integer
    pos_b as integer
    content as string

    pos_a = str_find(xml_LinesHolder[iterator],attribute,0)

    if pos_a > -1
        inc pos_a, len(attribute)+2
        pos_b = str_find(xml_LinesHolder[iterator],chr(66),pos_a)
        if pos_b = -1
            pos_b = str_find(xml_LinesHolder[iterator],"'",pos_a)
            if pos_b = -1
                content = "NULL"
            else
                content = str_substr(xml_LinesHolder[iterator],pos_a,pos_b)
            endif
        else
            content = str_substr(xml_LinesHolder[iterator],pos_a,pos_b)
        endif
    else
        content = "NULL"
    endif
endfunction content

function xml_isDeclaration(iterator as integer)
    pos_a as integer
    pos_b as integer
    content as string

    pos_a = str_find(xml_LinesHolder[iterator],"<?",0)
    if pos_a > -1
        pos_b = str_find(xml_LinesHolder[iterator]," ",pos_a)
        pos_c = str_find(xml_LinesHolder[iterator],"?>",pos_a)

        if pos_c<pos_b
            pos_b = pos_c
        endif

        if pos_a > -1 and pos_b > -1
            content$ = str_substr(xml_LinesHolder[iterator],pos_a+2,pos_b)
        endif
    else
        content = "NULL"
    endif

endfunction content$

//String Functions
function str_find(a as string, b as string, c as integer)
    place = -1
    if len(a) > 0
        lengthB = len(b)
        for i=c to len(a) step 1
            if (mid(a,i+1,lengthB) = b)
                place = i
                exit
            endif
        next i
    endif
endfunction place

function str_substr(a as string, startPos as integer, endPos as integer)
    returnValue as string
    if startPos=-1 or endPos=-1
        returnValue = "NULL"
    else
        returnValue = mid(a, startPos+1, endPos-startPos)
    endif
endfunction returnValue

